import java.util.concurrent.*;

public class CyclicBarrierTest {
    public static void main(String[] args) throws InterruptedException {

        // 假设每个班车有50个座位.
        CyclicBarrier cyclicBarrier = new CyclicBarrier(50, () -> System.out.println("满座了!  开始起飞....."));

        // 100人
        for (int i = 0; i < 100; i++) {
            new Thread(new RegularBus(cyclicBarrier, i)).start();
        }
    }


    /**
     * 班车
     */
    static class RegularBus implements Runnable {
        private final CyclicBarrier cyclicBarrier;

        /**
         * 员工工号
         */
        private final Integer ppId;

        RegularBus(CyclicBarrier cyclicBarrier, Integer poorPeopleId) {
            this.cyclicBarrier = cyclicBarrier;
            this.ppId = poorPeopleId;
        }

        @Override
        public void run() {
            if (ppId % 10 == 0) {
                System.out.println("工号为" + ppId + "的员工抢到了一个的座位.....");
            }
            try {
                cyclicBarrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }
}